<?php

namespace app\admin\controller;

class UserBase
{
    public function index()
    {
        $query = \app\model\User::field(['id', 'username', 'real_name', 'phone', 'created_at'])->with(['roles'])->order('id', 'desc');

        if ($username = input('username')) {
            $query->whereLike('username', '%' . $username . '%');
        }

        if ($real_name = input('realName')) {
            $query->whereLike('real_name', '%' . $real_name . '%');
        }

        if ($phone = input('phone')) {
            $query->whereLike('phone', '%' . $phone . '%');
        }

        if ($created_at = input('createdAt')) {
            $query->whereBetween('created_at', $created_at);
        }

        $models = $query->select();

        return json(['code' => 20000, 'data' => ['users' => $models]]);
    }

    public function delete()
    {
        $model = \app\model\User::destroy(input('id'));

        return json(['code' => 20000, 'msg' => '移除成功', 'data' => $model]);
    }

    public function update()
    {
        $model = \app\model\User::update(input());

        return json(['code' => 20000, 'msg' => '更新成功', 'data' => $model]);
    }

    public function create()
    {
        $model = \app\model\User::create(input());

        return json(['code' => 20000, 'msg' => '新增成功', 'data' => $model]);
    }

    public function detail()
    {
        $model = \app\model\User::field('id', 'username', 'real_name', 'phone')->where('id', input('id'))->find();

        return ['code' => 20000, 'data' => ['user' => $model]];
    }
}
